<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/30
 * Time: 13:43
 */

class Apiapplication extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model('api_model');
    }
    public function index($msg=''){
        $data['msg']=$msg;
        $data['typelist']=$this->api_model->apitypelist();
        $this->load->view('defaults/apirg',$data);
    }

    /**
     * 添加api
     */
    public function addapiapplication(){
        $gp=gp();
        $typename=$this->api_model->apitypelist('','',$gp['apitype']);
        if($gp) {
            $data = array(
                'name' => $gp['name'],
                'url' => $gp['url'],
                'apitype' => $gp['apitype'],
                'apitypename'=>$typename[0]['typename'],
                'addtime' => time(),
            );
            $ts=$this->api_model->apilist($gp['name'],$gp['apitype']);
            if($ts){
                js_goto('','请不要重复提交！！！','1');
            }else {
                $rq = $this->api_model->addapi($data);
                if ($rq) {
                    $this->index('提交成功！申请正在审核当中.....');
                }
            }
        }else{
            echo "我就问你想干啥！！！";
        }
    }

    /**
     * 获取token
     */
    public function GetToken(){
        $gp=gp();
        if(!$gp['grant_type']){
            $data['msg']=" grant_type is null";
            $data['code']=-30001;
            echo json_encode($data);
            exit();
        }
        if($gp['grant_type'] != 'client_credential'){
            $data['msg']=" grant_type Illegal";
            $data['code']=-30002;
            echo json_encode($data);
            exit();
        }
        if(!$gp['appId']){
            $data['msg']=" appId is null";
            $data['code']=-30003;
            echo json_encode($data);
            exit();
        }
        if(!$gp['secret']){
            $data['msg']=" secret is null";
            $data['code']=-30004;
            echo json_encode($data);
            exit();
        }
        $query = $this->api_model->apilist($gp['appId'],$gp['secret']);
        if ($query) {
                $strId = $this->getRandomStr($gp['appId'], 16);
                $strSecret = $this->getRandomStr($gp['secret'], 16);
                $data['access_token'] = sha1($strId . $strSecret) . sha1($strSecret) . sha1($strId);
                $data['expires_in'] = 7200;
                $access_token = $this->api_model->accreditapi($query[0]['id'], array('access_token' => $data['access_token'], 'tokentime' => time()));
                if ($access_token) {
                    echo json_encode($data);
                } else {
                    echo json_encode(array('msg' => 'system error','code'=>-30006));
                }
        } else {
            $data['msg'] = "appId or secret Illegal";
            $data['code'] = -30005;
            echo json_encode($data);
        }
    }

    /**
     * 随机生成规定长度字符串
     * @param $str_pol 字符串规则
     * @param $num 长度
     * @return string
     */
    function getRandomStr($str_pol,$num)
    {
        $str = "";
        $max = strlen($str_pol) - 1;
        for ($i = 0; $i < $num; $i++) {
            $str .= $str_pol[mt_rand(0, $max)];
        }
        return $str;
    }

    /**
     * 测试存款
     */
    function  TestDepositApi(){
        $gp=gp();
        $url = "http://sandbox.tomrepair.es/sc/maintainApi/DepositApi/PostDepositRecord";
        $token = $this->TestGetToken();
        echo $token;
        $data = array(
            'access_token' => $token,
            'data' => array(
                'customers_id' => 9,
                'time' => time(),
                'transactionId' => '6666666',
                'currency' => 'USD',
                'money' => '100000',
            ),
        );
        $result = CurlPost($url, $data);
        gsh_output($result);
    }

    /**
     * 测试token
     * @return mixed
     */
    function TestGetToken(){
        $url = "http://oms.tomrepair.es/maintainApi/Apiapplication/GetToken?appId=DepositRecord14523319238&secret=09a640196dfd72dfa33918944048e657aa9ab816&grant_type=client_credential";
        $data = UrlGet($url);
        return $data['access_token'];
    }

    /**
     * 测试添加账户接口
     */
    public function TestAddAccount(){
        $token=$this->TestGetToken();
        $url="http://oms.tomrepair.es/maintainApi/PostAccountInfo/AddAccount";
        $data=array(
            'access_token'=>$token,
            'data'=>array(
                'tr_id'=>'2',
                'email'=>'fdsfasdf',
                'name'=>'fdsafas',
                'company'=>''
            )
        );
        $result=CurlPost($url,$data);
        gsh_output($result);
    }
}